rm(list=ls())

sink("conjoint_ncse_plot.txt")

library(ggplot2)
library(Rmisc)
library(cjoint)
library(readstata13)

###############################
#### FIGURES IN MANUSCRIPT ####
###############################

### Figure 2: Effects of Candidate Attributes on Probability of Selection
d <- read.table("results/fig_2.txt",row.names=1,header=T)
d$var <- rownames(d)
colnames(d) <- c("pe","se","var")
d$upper <-d$pe + 1.96*d$se
d$lower <-d$pe - 1.96*d$se

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d$gruppe <- NA
d$gruppe[d$var %in% Gender]       <- "Gender"
d$gruppe[d$var %in% CCP]       <- "Political Affiliation"
d$gruppe[d$var %in% College]       <- "College Attended"
d$gruppe[d$var %in% Education]       <- "Education Level"
d$gruppe[d$var %in% Prize]       <- "Award Won in College"
d$gruppe[d$var %in% Work]       <- "Prior Work Experience"
d$gruppe[d$var %in% Father]       <- "Father's Occupation"

offset <- c("   ")
d$var[d$var %in% Gender] <- paste(offset,c("Female","Male"))
d$var[d$var %in% CCP] <- paste(offset,c("None","CCP Member"))
d$var[d$var %in% College] <- paste(offset,c("General College","Elite University"))
d$var[d$var %in% Education] <- paste(offset,c("Bachelor Degree","Master Degree"))
d$var[d$var %in% Prize] <- paste(offset,c("No Award", "Artistic Talent","Community Outreach","Academic Excellence","Student Leadership"))
d$var[d$var %in% Work] <- paste(offset,c("No Experience","Company Job","Government Job"))
d$var[d$var %in% Father] <- paste(offset,c("Private Sector Worker","SOE Worker (CCP Member)","Private Entrepreneur","Government Official"))

d$order <- 1:nrow(d)
dd <- data.frame(var= c("Gender:", " ",
                        "Political Affiliation:","  ",
                        "College Attended:", "   ",
                        "Education Level:", "    ",
                        "Award Won in College:", "     ",
                        "Prior Work Experience:", "",
                        "Father's Occupation:", "   "),
                 order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,13.1,13.5,16.1,16.5,20.1),
                 pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change: Probability of Being Selected")
p1 = ggplot(d,aes(y=pe,x=var))##,colour=gruppe))
p1 = p1 + coord_flip(ylim = c(-.4, .4))
p1 = p1 + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p1 = p1 + geom_pointrange(aes(ymin=lower,ymax=upper,width=.2),position="dodge",size=.6)
p1 = p1 + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4"))
p1 = p1 + scale_colour_discrete("Attribute:") + scale_x_discrete(name="")

theme_bw1 <- function(base_size = 11, base_family = "") {
  theme_grey(base_size = base_size, base_family = base_family) %+replace%
    theme(
      axis.text.x =       element_text(size = base_size*.9, colour = "black",  hjust = .5 , vjust=1),
      axis.text.y =       element_text(size = base_size, colour = "black", hjust = 0 , vjust=.5 ), # changes position of X axis text
      axis.ticks =        element_line(colour = "grey50"),
      axis.title.y =      element_text(size = base_size,angle=90,vjust=.01,hjust=.1),
      legend.position = "none"
    )
}

p1 = p1  + theme_bw1()

pdf("results/Fig2.pdf",width=6,height=8.5) 
print(p1)
dev.off()


### Figure 3: Effects of Candidate Attributes on Rating of Being “Suitable & Qualified”
d <- read.table("results/fig_3.txt",row.names=1,header=T)
d$var <- rownames(d)
colnames(d) <- c("pe","se","var")
d$upper <-d$pe + 1.96*d$se
d$lower <-d$pe - 1.96*d$se

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d$gruppe <- NA
d$gruppe[d$var %in% Gender]       <- "Gender"
d$gruppe[d$var %in% CCP]       <- "Political Affiliation"
d$gruppe[d$var %in% College]       <- "College Attended"
d$gruppe[d$var %in% Education]       <- "Education Level"
d$gruppe[d$var %in% Prize]       <- "Award Won in College"
d$gruppe[d$var %in% Work]       <- "Prior Work Experience"
d$gruppe[d$var %in% Father]       <- "Father's Occupation"

offset <- c("   ")
d$var[d$var %in% Gender] <- paste(offset,c("Female","Male"))
d$var[d$var %in% CCP] <- paste(offset,c("None","CCP Member"))
d$var[d$var %in% College] <- paste(offset,c("General College","Elite University"))
d$var[d$var %in% Education] <- paste(offset,c("Bachelor Degree","Master Degree"))
d$var[d$var %in% Prize] <- paste(offset,c("No Award", "Artistic Talent","Community Outreach","Academic Excellence","Student Leadership"))
d$var[d$var %in% Work] <- paste(offset,c("No Experience","Company Job","Government Job"))
d$var[d$var %in% Father] <- paste(offset,c("Private Sector Worker","SOE Worker (CCP Member)","Private Entrepreneur","Government Official"))

d$order <- 1:nrow(d)
dd <- data.frame(var= c("Gender:",
                        " ",
                        "Political Affiliation:",
                        "  ",
                        "College Attended:",
                        "   ",
                        "Education Level:",
                        "    ",
                        "Award Won in College:",
                        "     ",
                        "Prior Work Experience:",
                        "",
                        "Father's Occupation:",
                        ""
                        ),order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,13.1,13.5,16.1,16.5,20.1),
                 pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change: Rating on Being Suitable and Qualified")
p2 = ggplot(d,aes(y=pe,x=var))##,colour=gruppe))
p2 = p2 + coord_flip(ylim = c(-.4, .4))
p2 = p2 + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p2 = p2 +geom_pointrange(aes(ymin=lower,ymax=upper,width=.2),position="dodge",size=.6)
p2 = p2 + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4"))
p2 = p2 + scale_colour_discrete("Attribute:") + scale_x_discrete(name="")
p2 = p2  + theme_bw1()

pdf("results/Fig3.pdf",width=6,height=8.5) 
print(p2)
dev.off()


### Figure 4: Effects of Candidate Attributes on Ratings of Specific Competence Qualities
CIs <- function(d){
    colnames(d)[1:2] <- c("pe","se")
    d$upper <-d$pe + 1.96*d$se
    d$lower <-d$pe - 1.96*d$se
    return(d)
}
FillGroup <- function(d){  
    d$gruppe <- NA
    d$gruppe[d$var %in% Gender]       <- "Gender"
    d$gruppe[d$var %in% CCP]       <- "Political Affiliation"
    d$gruppe[d$var %in% College]       <- "College Attended"
    d$gruppe[d$var %in% Education]       <- "Education Level"
    d$gruppe[d$var %in% Prize]       <- "Award Won in College"
    d$gruppe[d$var %in% Work]       <- "Prior Work Experience"
    d$gruppe[d$var %in% Father]       <- "Father's Occupation" 
    ## reorder
    d <- rbind(d[d$var %in% Gender,],
               d[d$var %in% CCP,],
               d[d$var %in% College,],
               d[d$var %in% Education,],
               d[d$var %in% Prize,],
               d[d$var %in% Work,],
               d[d$var %in% Father,])
    return(d)
}
GetLabels <- function(d){
    offset <- c("   ")
    d$var[d$var %in% Gender] <- paste(offset,c("Female","Male"))
    d$var[d$var %in% CCP] <- paste(offset,c("None","CCP Member"))
    d$var[d$var %in% College] <- paste(offset,c("General College","Elite University"))
    d$var[d$var %in% Education] <- paste(offset,c("Bachelor Degree","Master Degree"))
    d$var[d$var %in% Prize] <- paste(offset,c("No Award", "Artistic Talent","Community Outreach","Academic Excellence","Student Leadership"))
    d$var[d$var %in% Work] <- paste(offset,c("No Experience","Company Job","Government Job"))
    d$var[d$var %in% Father] <- paste(offset,c("Private Sector Worker","SOE Worker (CCP Member)","Private Entrepreneur","Government Official"))
    return(d)
}

slevels <- c(1,2)
d1 <- read.table("results/fig_4a.txt",comment.char="")
d2 <- read.table("results/fig_4b.txt",comment.char="")
d <- rbind(d1, d2)
rows <- rownames(d1)
p <- nrow(d1)
d$subset      <- rep(1:2,each=p)
d$subsetlabel <- rep(c("Leadership Quality", "Task Implementation"),each=p)

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d<- CIs(d)
d$var <- rep(rows,2)
d <- FillGroup(d)
d$order <- 1:nrow(d)
d <- d[order(d$subset,d$order),]
d$order <- 1:nrow(d)
d <- GetLabels(d)
d <- d[order(d$order),]

dd <- data.frame(order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,13.1,13.5,16.1,16.5),
                 pe=1,se=1,upper=1,lower=1,gruppe=NA,subset=NA,
                 subsetlabel="Leadership Quality")
dd$var= c("Gender:",
       " ",
       "Political Affiliation:",
       "  ",
       "College Attended:",
       "   ",
       "Education Level:",
       "    ",
       "Award Won in College:",
       "     ",
       "Prior Work Experience:",
       "      ",
       "Father's Occupation:" )
dd2 <- dd
dd2$order <- dd2$order + 20
dd2$subsetlabel <-"Task Implementation"
d <- rbind(d,dd, dd2)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])
rownames(d)<-c()

yylab  <- c("Change: Rating on Specific Candidate Qualities")
p3 = ggplot(d ,aes(y=pe,x=var))##,colour=gruppe))
p3 = p3 + facet_grid(.~subsetlabel)
p3 = p3 + coord_flip(ylim = c(-.5, .5))
p3 = p3 + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p3 = p3 + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),position="dodge",size=.6)
p3 = p3 + scale_y_continuous(name=yylab,breaks=round(seq(-.6,.6,.2),1),
                             labels=c("","-.4","-.2","0",".2",".4",""))
p3 = p3 + scale_x_discrete(name="") + scale_colour_discrete("")
p3 = p3  + theme_bw1()

pdf("results/Fig4.pdf",width=10,height=8.5) 
print(p3)
dev.off()



#############################
#### FIGURES IN APPENDIX ####
#############################

### Figure A3: Interaction Effects of Candidate’s Political Connections with Respondent’s Work Unit
d <- read.dta13("conjoint_ncse.dta")
d <- na.omit(d[ , c("ObsID","win","male","ccp","eliteuniv","postgrad","prize","workexp","fatherjob", "res_workunit")])
choice.workunit <- amce(win ~ male + ccp + eliteuniv + postgrad + prize + workexp + fatherjob + res_workunit:fatherjob, 
                        design="uniform", data=d, cluster=TRUE, respondent.id = "ObsID")
summary(choice.workunit) ## relevant coefficients are manually copied and stored in "fig_a3.csv"

d_wkunit <- read.csv("fig_a3.csv",row.names=1,header=T)
d_wkunit$var <- rownames(d_wkunit)
colnames(d_wkunit) <- c("coef","se","lower","upper","var")
d_wkunit$order <- 1:nrow(d_wkunit)
d_wkunit <-d_wkunit[order(d_wkunit$order),]
d_wkunit$var <- factor(d_wkunit$var,levels=unique(d_wkunit$var)[length(d_wkunit$var):1])

yylab  <- c("Change: Probability of Being Selected")
p_wkunit = ggplot(d_wkunit,aes(y=coef,x=var))
p_wkunit = p_wkunit + coord_flip(ylim= c(-.4,.4))
p_wkunit = p_wkunit + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p_wkunit = p_wkunit + geom_pointrange(aes(ymin=lower,ymax=upper,width=.2),position="dodge",size=.4)
p_wkunit = p_wkunit + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4"))
p_wkunit = p_wkunit + scale_colour_discrete("Attribute:") + scale_x_discrete(name="")

pdf("results/FigA3.pdf",width=6,height=6) 
print(p_wkunit)
dev.off()


### Figure A4: Heterogeneous Effects of Candidate Attributes on Probability of Selection

# by gender
k=2
filename <- "subplot_gender"
subsetnlabel <- ""
slevels <- c(1,2)
d1 <- read.table("results/fig_a4_gender1.txt",comment.char="")
d2 <- read.table("results/fig_a4_gender2.txt",comment.char="")
rows <- rownames(d1)
p <- nrow(d1)
d <- rbind(d1, d2)
d$subset      <- rep(1:2,each=p)
d$subsetlabel <- rep(c("Male","Female"),each=p)

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d<- CIs(d)
d$var <- rep(rows,k)
d <- FillGroup(d)
d$order <- 1:nrow(d)
d <- d[order(d$subset,d$order),]
d$order <- 1:nrow(d)
d <- GetLabels(d)
d <- d[order(d$order),]

dd <- data.frame(var= c("Gender:",
                        " ",
                        "Political Affiliation:",
                        "  ",
                        "College Attended:",
                        "   ",
                        "Education Level:",
                        "    ",
                        "Award Won in College:",
                        "     ",
                        "Prior Work Experience:",
                        "        ",
                        "Father's Occupation:"
                        ),order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,
                                  13.1,13.5,16.1,16.5),
                 pe=1,se=1,upper=1,lower=1,gruppe=NA,subset=NA,subsetlabel="Male")
dd2 <- dd
dd2$order <- dd2$order + 20
dd2$subsetlabel <-"Female" 
d <- rbind(d,dd, dd2)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("a. Probability of Being Selected by Respondent's Gender")
p_gender = ggplot(d ,aes(y=pe,x=var))##,colour=gruppe))
p_gender = p_gender + facet_grid(.~subsetlabel)
p_gender = p_gender + coord_flip(ylim = c(-.4, .4))
p_gender = p_gender + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p_gender = p_gender +geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),position="dodge",size=.6)
p_gender = p_gender + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4"))
p_gender = p_gender + scale_x_discrete(name="") + scale_colour_discrete("")
p_gender = p_gender + theme_bw1()

# by age
k=2
filename <- "subplot_age"
subsetnlabel <- ""
slevels <- c(1,2)
d1 <- read.table("results/fig_a4_age1.txt",comment.char="")
d2 <- read.table("results/fig_a4_age2.txt",comment.char="")
rows <- rownames(d1)
p <- nrow(d1)
d <- rbind(d1, d2)
d$subset      <- rep(1:2,each=p)
d$subsetlabel <- rep(c("Below Age 30","Age 30 & Above"),each=p)

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d<- CIs(d)
d$var <- rep(rows,k)
d <- FillGroup(d)
d$order <- 1:nrow(d)
d <- d[order(d$subset,d$order),]
d$order <- 1:nrow(d)
d <- GetLabels(d)
d <- d[order(d$order),]

dd <- data.frame(var= c("Gender:",
                        " ",
                        "Political Affiliation:",
                        "  ",
                        "College Attended:",
                        "   ",
                        "Education Level:",
                        "    ",
                        "Award Won in College:",
                        "     ",
                        "Prior Work Experience:",
                        "        ",
                        "Father's Occupation:"
),order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,
          13.1,13.5,16.1,16.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA,subset=NA,subsetlabel="Below Age 30")
dd2 <- dd
dd2$order <- dd2$order + 20
dd2$subsetlabel <-"Age 30 & Above" 
d <- rbind(d,dd, dd2)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("b. Probability of Being Selected by Respondent's Age")
p_age = ggplot(d ,aes(y=pe,x=var))##,colour=gruppe))
p_age = p_age + facet_grid(.~subsetlabel)
p_age = p_age + coord_flip(ylim = c(-.4, .4))
p_age = p_age + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p_age = p_age +geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),position="dodge",size=.6)
p_age = p_age + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4"))
p_age = p_age + scale_x_discrete(name="") + scale_colour_discrete("")
p_age = p_age + theme_bw1()


# by rank
k=2
filename <- "subplot_rank"
subsetnlabel <- ""
slevels <- c(1,2)
d1 <- read.table("results/fig_a4_rank1.txt",comment.char="")
d2 <- read.table("results/fig_a4_rank2.txt",comment.char="")
rows <- rownames(d1)
p <- nrow(d1)
d <- rbind(d1, d2)
d$subset      <- rep(1:2,each=p)
d$subsetlabel <- rep(c("Lower Rank","Higher Rank"),each=p)

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d<- CIs(d)
d$var <- rep(rows,k)
d <- FillGroup(d)
d$order <- 1:nrow(d)
d <- d[order(d$subset,d$order),]
d$order <- 1:nrow(d)
d <- GetLabels(d)
d <- d[order(d$order),]

dd <- data.frame(var= c("Gender:",
                        " ",
                        "Political Affiliation:",
                        "  ",
                        "College Attended:",
                        "   ",
                        "Education Level:",
                        "    ",
                        "Award Won in College:",
                        "     ",
                        "Prior Work Experience:",
                        "        ",
                        "Father's Occupation:"
),order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,
          13.1,13.5,16.1,16.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA,subset=NA,subsetlabel="Lower Rank")
dd2 <- dd
dd2$order <- dd2$order + 20
dd2$subsetlabel <-"Higher Rank" 
d <- rbind(d,dd, dd2)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("c. Probability of Being Selected by Respondent's Bureaucratic Rank")
p_rank = ggplot(d ,aes(y=pe,x=var))##,colour=gruppe))
p_rank = p_rank + facet_grid(.~subsetlabel)
p_rank = p_rank + coord_flip(ylim = c(-.4, .4))
p_rank = p_rank + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p_rank = p_rank +geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),position="dodge",size=.6)
p_rank = p_rank + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4"))
p_rank = p_rank + scale_x_discrete(name="") + scale_colour_discrete("")
p_rank = p_rank + theme_bw1()

# by authority over personnel decisions
k=2
filename <- "subplot_authority"
subsetnlabel <- ""
slevels <- c(1,2)
d1 <- read.table("results/fig_a4_authority1.txt",comment.char="")
d2 <- read.table("results/fig_a4_authority2.txt",comment.char="")
rows <- rownames(d1)
p <- nrow(d1)
d <- rbind(d1, d2)
d$subset      <- rep(1:2,each=p)
d$subsetlabel <- rep(c("Has Authority","Has No Authority"),each=p)

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d<- CIs(d)
d$var <- rep(rows,k)
d <- FillGroup(d)
d$order <- 1:nrow(d)
d <- d[order(d$subset,d$order),]
d$order <- 1:nrow(d)
d <- GetLabels(d)
d <- d[order(d$order),]

dd <- data.frame(var= c("Gender:",
                        " ",
                        "Political Affiliation:",
                        "  ",
                        "College Attended:",
                        "   ",
                        "Education Level:",
                        "    ",
                        "Award Won in College:",
                        "     ",
                        "Prior Work Experience:",
                        "        ",
                        "Father's Occupation:"
),order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,
          13.1,13.5,16.1,16.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA,subset=NA,subsetlabel="Has Authority")
dd2 <- dd
dd2$order <- dd2$order + 20
dd2$subsetlabel <-"Has No Authority" 
d <- rbind(d,dd, dd2)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("d. Probability of Being Selected by Respondent's Authority over Personnel Decision")
p_authority = ggplot(d ,aes(y=pe,x=var))##,colour=gruppe))
p_authority = p_authority + facet_grid(.~subsetlabel)
p_authority = p_authority + coord_flip(ylim = c(-.4, .4))
p_authority = p_authority + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p_authority = p_authority +geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),position="dodge",size=.6)
p_authority = p_authority + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4"))
p_authority = p_authority + scale_x_discrete(name="") + scale_colour_discrete("")
p_authority = p_authority + theme_bw1()

pdf("results/FigA4.pdf",width=21,height=14)
multiplot(p_gender, p_rank, p_age, p_authority, cols=2)
dev.off()


### Figure A5: Effects of Candidate Attributes on Probability of Selection with Respondent FEs and REs

slevels <- c(1,2)
d1 <- read.table("results/fig_a5a.txt",comment.char="")
d2 <- read.table("results/fig_a5b.txt",comment.char="")
d <- rbind(d1, d2)
rows <- rownames(d1)
p <- nrow(d1)
d$subset      <- rep(1:2,each=p)
d$subsetlabel <- rep(c("Fixed Effects","Random Effects"),each=p)

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d<- CIs(d)
d$var <- rep(rows,2)
d <- FillGroup(d)
d$order <- 1:nrow(d)
d <- d[order(d$subset,d$order),]
d$order <- 1:nrow(d)
d <- GetLabels(d)
d <- d[order(d$order),]

dd <- data.frame(order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,13.1,13.5,16.1,16.5),
                 pe=1,se=1,upper=1,lower=1,gruppe=NA,subset=NA,
                 subsetlabel="Fixed Effects")
dd$var= c("Gender:",
          " ",
          "Political Affiliation:",
          "  ",
          "College Attended:",
          "   ",
          "Education Level:",
          "    ",
          "Award Won in College:",
          "     ",
          "Prior Work Experience:",
          "      ",
          "Father's Occupation:" )
dd2 <- dd
dd2$order <- dd2$order + 20
dd2$subsetlabel <-"Random Effects" 
d <- rbind(d,dd, dd2)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])
rownames(d)<-c()

yylab  <- c("Change: Probability of Being Selected")
p_fere = ggplot(d ,aes(y=pe,x=var))##,colour=gruppe))
p_fere = p_fere + facet_grid(.~subsetlabel)
p_fere = p_fere + coord_flip(ylim = c(-.4, .4))
p_fere = p_fere + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p_fere = p_fere + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),position="dodge",size=.6)
p_fere = p_fere + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),
                                     labels=c("-.4","-.2","0",".2",".4"))
p_fere = p_fere + scale_x_discrete(name="") + scale_colour_discrete("")
p_fere = p_fere  + theme_bw1()

pdf("results/FigA5.pdf",width=10,height=8.5) 
print(p_fere)
dev.off()


### Figure A6: Effects of Candidate Attributes on Probability of Selection for Online and Offline Subgroups

slevels <- c(1,2)
d1 <- read.table("results/fig_a6a.txt",comment.char="")
d2 <- read.table("results/fig_a6b.txt",comment.char="")
d <- rbind(d1, d2)
rows <- rownames(d1)
p <- nrow(d1)
d$subset      <- rep(1:2,each=p)
d$subsetlabel <- rep(c("Online Sample","Offline Sample"),each=p)

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d<- CIs(d)
d$var <- rep(rows,2)
d <- FillGroup(d)
d$order <- 1:nrow(d)
d <- d[order(d$subset,d$order),]
d$order <- 1:nrow(d)
d <- GetLabels(d)
d <- d[order(d$order),]

dd <- data.frame(order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,13.1,13.5,16.1,16.5),
                 pe=1,se=1,upper=1,lower=1,gruppe=NA,subset=NA,
                 subsetlabel="Online Sample")
dd$var= c("Gender:",
          " ",
          "Political Affiliation:",
          "  ",
          "College Attended:",
          "   ",
          "Education Level:",
          "    ",
          "Award Won in College:",
          "     ",
          "Prior Work Experience:",
          "      ",
          "Father's Occupation:" )
dd2 <- dd
dd2$order <- dd2$order + 20
dd2$subsetlabel <-"Offline Sample" 
d <- rbind(d,dd, dd2)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])
rownames(d)<-c()

yylab  <- c("Change: Probability of Being Selected")
p_subsample = ggplot(d ,aes(y=pe,x=var))##,colour=gruppe))
p_subsample = p_subsample + facet_grid(.~subsetlabel)
p_subsample = p_subsample + coord_flip(ylim = c(-.4, .4))
p_subsample = p_subsample + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p_subsample = p_subsample + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),position="dodge",size=.6)
p_subsample = p_subsample + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),
                                               labels=c("-.4","-.2","0",".2",".4"))
p_subsample = p_subsample + scale_x_discrete(name="") + scale_colour_discrete("")
p_subsample = p_subsample  + theme_bw1()

pdf("results/FigA6.pdf",width=10,height=8.5) 
print(p_subsample)
dev.off()


### Figure A7: Effects of Candidate Attributes on Probability of Selection by Task Number

k=5
subsetnlabel <- ""
slevels <- c(1:5)

d <- read.table("results/fig_a7_1.txt",comment.char="")
rows <- rownames(d)
p <- nrow(d)
for(i in 2:k){
  d <-   rbind(d,read.table(paste("results/fig_a7_",i,".txt",sep=""),comment.char=""))
}
d$subset      <- rep(1:k,each=p)
d$subsetlabel <- paste(subsetnlabel, rep(paste("Pair ",1:5),each=p))

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d<- CIs(d)
d$var <- rep(rows,k)
d <- FillGroup(d)
d$order <- 1:nrow(d)
d <- d[order(d$subset,d$order),]
d$order <- 1:nrow(d)
d <- GetLabels(d)
d <- d[order(d$order),]

dd <- data.frame(var= c("Gender:",
                        " ",
                        "Political Affiliation:",
                        "  ",
                        "College Attended:",
                        "   ",
                        "Education Level:",
                        "    ",
                        "Award Won in College:",
                        "     ",
                        "Prior Work Experience:",
                        "        ",
                        "Father's Occupation:"
),order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,
          13.1,13.5,16.1,16.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA,subset=NA,subsetlabel=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

d$subsetlabel[is.na(d$subsetlabel)] <- unique(d$subsetlabel)[2]
d$subsetlabel <- factor(d$subsetlabel,levels=unique(d$subsetlabel))

yylab  <- c("Change: Probability of Being Selected")
p_task = ggplot(d ,aes(y=pe,x=var))##,colour=gruppe))
p_task = p_task + facet_grid(.~subsetlabel)
p_task = p_task + coord_flip(ylim = c(-.5, .5))
p_task = p_task + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p_task = p_task + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),position="dodge",size=.6)
p_task = p_task + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),
                                     labels=c("-.4","-.2","0",".2",".4"))
p_task = p_task + scale_x_discrete(name="") + scale_colour_discrete("")
p_task = p_task  + theme_bw1()

pdf("results/FigA8.pdf",width=13,height=8.5) 
print(p_task)
dev.off()


### Figure A8: Effects of Candidate Attributes on Probability of Selection by Profile Position
slevels <- c(1,2)
d1 <- read.table("results/fig_a8a.txt",comment.char="")
d2 <- read.table("results/fig_a8b.txt",comment.char="")
d <- rbind(d1, d2)
rows <- rownames(d1)
p <- nrow(d1)
d$subset      <- rep(1:2,each=p)
d$subsetlabel <- rep(c("Profile 1","Profile 2"),each=p)

Gender     <- paste(c("0b",1),".male",sep="")
CCP     <- paste(c("0b",1),".ccp",sep="")
College    <- paste(c("0b",1),".eliteuniv",sep="")
Education   <- paste(c("0b",1),".postgrad",sep="")
Prize       <- paste(c("0b",1:4),".prize",sep="")
Work       <- paste(c("0b",1:2),".workexp",sep="")
Father     <- paste(c("0b",1:3),".fatherjob",sep="")

d<- CIs(d)
d$var <- rep(rows,2)
d <- FillGroup(d)
d$order <- 1:nrow(d)
d <- d[order(d$subset,d$order),]
d$order <- 1:nrow(d)
d <- GetLabels(d)
d <- d[order(d$order),]

dd <- data.frame(order=c(.5,2.1,2.5,4.1,4.5,6.1,6.5,8.1,8.5,13.1,13.5,16.1,16.5),
                 pe=1,se=1,upper=1,lower=1,gruppe=NA,subset=NA,
                 subsetlabel="Profile 1")
dd$var= c("Gender:",
          " ",
          "Political Affiliation:",
          "  ",
          "College Attended:",
          "   ",
          "Education Level:",
          "    ",
          "Award Won in College:",
          "     ",
          "Prior Work Experience:",
          "      ",
          "Father's Occupation:" )
dd2 <- dd
dd2$order <- dd2$order + 20
dd2$subsetlabel <-"Profile 2" 
d <- rbind(d,dd, dd2)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])
rownames(d)<-c()

yylab  <- c("Change: Probability of Being Selected")
p_profile = ggplot(d ,aes(y=pe,x=var))##,colour=gruppe))
p_profile = p_profile + facet_grid(.~subsetlabel)
p_profile = p_profile + coord_flip(ylim = c(-.4, .4))
p_profile = p_profile + geom_hline(yintercept = 0,size=.5,colour="black",linetype="dotted") 
p_profile = p_profile + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),position="dodge",size=.6)
p_profile = p_profile + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),
                                           labels=c("-.4","-.2","0",".2",".4"))
p_profile = p_profile + scale_x_discrete(name="") + scale_colour_discrete("")
p_profile = p_profile  + theme_bw1()

pdf("results/FigA8.pdf",width=10,height=8.5) 
print(p_profile)
dev.off()

sink()
